- name: check/create instance
  hosts: 127.0.0.1
  gather_facts: False

  vars_files:
    - nova_cloud_vars.yml

  vars:
    # _OS_AUTH_OPTS: "--os-auth-url {{OS_AUTH_URL}} --os-username {{OS_USERNAME}} --os-password {{OS_PASSWORD}} --os-tenant-name {{OS_TENANT_NAME}} --os-tenant-id {{OS_TENANT_ID}} "

    keypair: buildsys
    max_spawn_time: 1100
    spawning_vm_user: "fedora"
    image_name: "copr-builder-x86_64-f27"

  tasks:
    - name: generate builder name
      local_action: set_fact vm_name="Copr_builder_{{ 999999999 | random }}"

    - debug: msg="vm_name={{ vm_name }}"
    - include: "spinup_nova_task.yml"

    - debug: msg="VM_IP={{ builder_ip }}"

    - name: wait for he host to be hot
      local_action: wait_for host={{ builder_ip }} port=22 delay=1 timeout=1100

    - name: wait until ssh is available
      local_action:  shell false; until [ "$?" -eq "0" ]; do sleep 2; ssh -o PasswordAuthentication=no {{ spawning_vm_user|default('fedora') }}@{{ builder_ip }} 'echo foobar' 2>/dev/null; done
      async: 600
      poll: 2


- name: provision builder
  hosts: builder_temp_group
  gather_facts: True
  sudo: True
  user: fedora

  vars_files:
    - nova_cloud_vars.yml

  vars:
    # change this to True if you need to create new base image from snapshot
    prepare_base_image: False

  tasks:
  - include: "provision_builder_tasks.yml"

  - name: disable offloading
    command: ethtool -K eth0 tso off gro off gso off

    # ensure glibc and glibc.i686 are of the the latest version here
    # we need glibc.i686 and nosync.i686 so that nosync works for i386 and i586 builds
  - name: make sure latest nosync and nosync.i686 and glibc
    dnf: state=latest pkg={{ item }}
    with_items:
      - glibc
      - glibc.i686
      - nosync
      - nosync.i686
    when: prepare_base_image
